# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : code01.py
# @Time     : 2024/4/6 16:41
# @Author   : Robot-Zsj
"""
description: 
"""
import pdb
import re

IN = '123#4$5#67$78'

OUT = 0

p = re.compile("(\\d+)\\$(\\d+)")


def main():
    string = input()
    while True:
        m = p.search(string)
        # 扫描整个 string 查找正则表达式 pattern 产生匹配的第一个位置，并返回相应的 Match。
        # 如果字符串中没有与模式匹配的位置则返回 None；请注意这不同于在字符串的某个位置上找到零长度匹配。
        if m:
            subS = m.group()
            x = int(m.group(1))
            y = int(m.group(2))
            string = string.replace(subS, str(3 * x + y + 2))
        else:
            break
    arr = list(map(int, string.split("#")))
    x = arr[0]
    for y in arr[1:]:
        x = 2 * x + 3 * y + 4
    return x


def draft():
    s = input()
    p = re.compile("(\\d+)\\$(\\d+)")
    while True:
        m = p.search(s)
        if m:
            subS = m.group()
            x = int(m.group(1))
            y = int(m.group(2))
            s = s.replace(subS, str(3 * x + y + 2))
        else:
            break
    arr = s.split('#')
    x = int(arr[0])
    for y in arr[1:]:
        x = x * 2 + int(y) * 3 + 4
    return x


print(main())
print(draft())
